package br.com.sgmf.web.converter;

import br.com.sgmf.model.Modelo;
import br.com.sgmf.service.ModeloService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

@Component("modeloConverter")
public class ModeloConverter implements Converter {

    @Autowired
    private ModeloService modeloService;

    @Override
    public Modelo getAsObject(FacesContext context, UIComponent component, String value) {
        if (StringUtils.isNotBlank(value)) {
            return modeloService.findById(Integer.valueOf(value));
        }

        return null;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value != null && value instanceof Modelo) {
            Modelo Modelo = (Modelo) value;
            return String.valueOf(Modelo.getId());
        }

        return null;
    }

}
